#include <stdio.h>
#include <stdlib.h>
#include "SDL/SDL.h"
#include <SDL/SDL_ttf.h>
#include <time.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

int main ( int argc, char** argv )
{
    SDL_Surface *screen,*image,*perso,*cartable=NULL,*pantalon=NULL,*chaussure=NULL,*chemise=NULL;
    SDL_Rect posimage,posperso,poscartable,pospantalon,poschaussure,poschemise;
    clock_t  start, fin;
    float temps=0;
    int done=0;
    char s[20],s1[20];

    SDL_Init( SDL_INIT_VIDEO );
    screen = SDL_SetVideoMode(741, 464, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
    image = IMG_Load("Images/background.jpg");
    perso = IMG_Load("Images/1.png");
    cartable = IMG_Load("Images/8.png");
    pantalon = IMG_Load("Images/9.png");
    chaussure = IMG_Load("Images/7.png");
    chemise = IMG_Load("Images/6.png");

    posimage.x = 0;
    posimage.y = 0;
    posperso.x = (741 - perso->w)/2;
    posperso.y = (464 - perso->h);
    poscartable.x=660;
    poscartable.y=400;
    pospantalon.x=0;
    pospantalon.y=434;
    poschaussure.x=475;
    poschaussure.y=115;
    poschemise.x=235;
    poschemise.y=220;

    start = clock();

    while ( (!done) && (temps<=15000000) )
    {
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                done = 1;
                break;

            case SDL_KEYDOWN:
                if (event.key.keysym.sym == SDLK_ESCAPE)
                    done = 1;
                break;

            case SDL_MOUSEBUTTONDOWN:
                if (event.button.button == SDL_BUTTON_LEFT)
                {
                    if ((event.button.x >=670 ) && (event.button.x <=700 ))
                    {
                        if ((event.button.y >=407  ) && (event.button.y <=454  ))
                        {
                            cartable = IMG_Load("Images/4.png");
                            poscartable.x=posperso.x;
                            poscartable.y=posperso.y;
                        }
                    }
                    else if ((event.button.x >=4 ) && (event.button.x <=50 ))
                    {
                        if ((event.button.y >=440  ) && (event.button.y <=460  ))
                        {
                            pantalon = IMG_Load("Images/3.png");
                            pospantalon.x=posperso.x;
                            pospantalon.y=posperso.y;
                        }
                    }
                    else if ((event.button.x >=482 ) && (event.button.x <=515 ))
                    {
                        if ((event.button.y >=122  ) && (event.button.y <=135  ))
                        {
                            chaussure = IMG_Load("Images/5.png");
                            poschaussure.x=posperso.x;
                            poschaussure.y=posperso.y;
                        }
                    }
                    else if ((event.button.x >=237 ) && (event.button.x <=280 ))
                    {
                        if ((event.button.y >=225  ) && (event.button.y <=250  ))
                        {
                            chemise = IMG_Load("Images/2.png");
                            poschemise.x=posperso.x;
                            poschemise.y=posperso.y;
                        }
                    }
                }
                break;
            }
        }

        fin= clock();
        temps = fin - start;
        if (temps<=9999)
        {
            if (temps<=999)
                sprintf(s1,"0");
            else if ((temps>=1000)&&(temps<=1999))
                sprintf(s1,"1");
            else if ((temps>=2000)&&(temps<=2999))
                sprintf(s1,"2");
            else if ((temps>=3000)&&(temps<=3999))
                sprintf(s1,"3");
            else if ((temps>=4000)&&(temps<=4999))
                sprintf(s1,"4");
            else if ((temps>=5000)&&(temps<=5999))
                sprintf(s1,"5");
            else if ((temps>=6000)&&(temps<=6999))
                sprintf(s1,"6");
            else if ((temps>=7000)&&(temps<=7999))
                sprintf(s1,"7");
            else if ((temps>=8000)&&(temps<=8999))
                sprintf(s1,"8");
            else if ((temps>=9000)&&(temps<=9999))
                sprintf(s1,"9");
        }
        else
            sprintf(s1,"%f",temps);
        sscanf(s1,"%2s",s);
        SDL_WM_SetCaption(s, NULL);

        SDL_BlitSurface(image, 0, screen, &posimage);
        SDL_BlitSurface(perso, 0, screen, &posperso);
        SDL_BlitSurface(chemise, 0, screen, &poschemise);
        SDL_BlitSurface(pantalon, 0, screen, &pospantalon);
        SDL_BlitSurface(chaussure, 0, screen, &poschaussure);
        SDL_BlitSurface(cartable, 0, screen, &poscartable);
        SDL_Flip(screen);
    }

    SDL_FreeSurface(image);
    SDL_FreeSurface(perso);
    SDL_FreeSurface(cartable);
    SDL_FreeSurface(pantalon);
    SDL_FreeSurface(chaussure);
    SDL_FreeSurface(chemise);
    return 0;
}
